﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mighty.Common;
using System.Xml.Serialization;
using System.IO;
using Mighty.Brain;
using System.Collections;

namespace mighty
{
    public partial class ConfigForm : Form
    {
        public Config config = new Config();
        public MightyModule mightyMoidule = new MightyModule();
        public Hashtable moduleHash = new Hashtable();

        public ConfigForm()
        {
            InitializeComponent();
            

            config = getConfig();

            txtPlayer0.Text = config.nick[0];
            txtPlayer1.Text = config.nick[1];
            txtPlayer2.Text = config.nick[2];
            txtPlayer3.Text = config.nick[3];
            txtPlayer4.Text = config.nick[4];



            mightyMoidule.LoadDLL();

            try
            {
                // combobox 설정
                

                foreach (ModuleInfo module in mightyMoidule.moduleHash.Values)
                {
                    comboAI0.Items.Add(module.moduleID);
                    comboAI1.Items.Add(module.moduleID);
                    comboAI2.Items.Add(module.moduleID);
                    comboAI3.Items.Add(module.moduleID);
                    comboAI4.Items.Add(module.moduleID);
                }

                
                  
                comboAI0.Text = config.AI[0];
                comboAI1.Text = config.AI[1];
                comboAI2.Text = config.AI[2];
                comboAI3.Text = config.AI[3];
                comboAI4.Text = config.AI[4];


                if (config.AI[0] == null)
                    comboAI0.Text = "Brain.BasicBrain";
                if (config.AI[1] == null)
                    comboAI1.Text = "Brain.BasicBrain";
                if (config.AI[2] == null)
                    comboAI2.Text = "Brain.BasicBrain";
                if (config.AI[3] == null)
                    comboAI3.Text = "Brain.BasicBrain";
                if (config.AI[4] == null)
                    comboAI4.Text = "Brain.BasicBrain";  


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        public static Config getConfig()
        {
            Config c = new Config();

            try
            {

                FileStream fs = new FileStream("config.cfg", FileMode.Open, FileAccess.Read);
                StreamReader reader = new StreamReader(fs, Encoding.GetEncoding("euc-kr"));
                string str = "";
              

                while (!reader.EndOfStream)
                {
                    str += reader.ReadLine();

                    if (str == null)  
                    {
                        break;
                    }
                }
                reader.Close();
                fs.Close();

                XmlSerializer m = new XmlSerializer(typeof(Config));
                StringReader r = new StringReader(str);
                c = (Config)m.Deserialize(r);
                r.Close();

                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return c;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
          

            config.nick[0] = txtPlayer0.Text;
            config.nick[1] = txtPlayer1.Text;
            config.nick[2] = txtPlayer2.Text;
            config.nick[3] = txtPlayer3.Text;
            config.nick[4] = txtPlayer4.Text;

            config.AI[0] = comboAI0.Text;
            config.AI[1] = comboAI1.Text;
            config.AI[2] = comboAI2.Text;
            config.AI[3] = comboAI3.Text;
            config.AI[4] = comboAI4.Text;

            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(Config));
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                xs.Serialize(sw, config);

                sw.Close();

                FileStream fs = new FileStream("config.cfg", FileMode.Create,FileAccess.Write);
                byte[] data = Encoding.Default.GetBytes(sb.ToString());

                for (int i = 0; i < data.Length; i++)
                {
                    fs.WriteByte(data[i]);
                }
                fs.Close();


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            this.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            for(int i = 0 ; i< 5; i++)
            {
                config.score[i] = (int)numResetScore.Value;
            }

            MessageBox.Show("초기화되었습니다.");
        }



       

       
    }

    public class Config
    {
        public string[] nick = new string[5];
        public string[] AI = new string[5];
        public int[] score = new int[5];
        
    }
}
